﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;
using SharpDiary.Core.Controls;
using SharpDiary.Core.Data;

namespace SharpDiary.Reminder
{
    public partial class ReminderInfoListItemControl : ListItemControlBase
    {
        private ReminderInfo _reminder;

        public ReminderInfo Reminder
        {
            get { return _reminder; }
            set
            {
                _reminder = value;
                DisplayItemInfo();
            }
        }

        private int _summaryTextMaxLength = 20;

        public int SummaryTextMaxLength
        {
            get { return _summaryTextMaxLength; }
            set { _summaryTextMaxLength = value; }
        }

        public ReminderInfoListItemControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 计算两个日期的间隔。
        /// </summary>
        /// <param name="fromDatetime"></param>
        /// <param name="toDatetime"></param>
        /// <returns></returns>
        private string GetBetweenTimeSpan(DateTime fromDatetime, DateTime toDatetime)
        {
            TimeSpan timeSpan = toDatetime - fromDatetime;
            if (timeSpan.TotalMinutes <= 0) { return string.Empty; }

            string days = timeSpan.Days > 0 ? timeSpan.Days.ToString() + "天" : string.Empty;
            string hours = timeSpan.Hours > 0 ? timeSpan.Hours.ToString() + "小时" : string.Empty;
            string mins = timeSpan.Minutes > 0 ? timeSpan.Minutes.ToString() + "分" : string.Empty;
            string secs = timeSpan.Seconds > 0 ? timeSpan.Seconds.ToString() + "秒" : string.Empty;

            return days + hours + mins + secs;
        }

        protected override void DisplayItemInfo()
        {
            base.DisplayItemInfo();

            if (_reminder == null) { return; }

            string between = GetBetweenTimeSpan(_reminder.AlarmDatetime, DateTime.Now);
            Title = _reminder.Name + ((ReminderType)_reminder.ReminderType == ReminderType.DailyTime || string.IsNullOrEmpty(between) ? string.Empty : " (已过期: " + between + "+)");

            Description = Utils.GetSummaryText(_reminder.Detail, _summaryTextMaxLength);
            CreatedAndUpdateDatetime = "提醒时间: " + 
                (((ReminderType)_reminder.ReminderType) == ReminderType.DateAndTime ? _reminder.AlarmDatetime.ToString()
                : "每天 " + _reminder.AlarmDatetime.ToString("HH:mm"));
            onOffButtonControl.Checked = _reminder.Status == 1;
            UpdateLogo(_reminder.Status);
        }

        /// <summary>
        /// 更新图示。
        /// </summary>
        private void UpdateLogo(Int64 reminderStatus)
        {
            ImageAboveLogo = (_reminder.Status == 1 ?
                global::SharpDiary.Reminder.Properties.Resources.Alarm32X32 :
                global::SharpDiary.Reminder.Properties.Resources.alarm_disabled);
        }

        /// <summary>
        /// 删除提醒信息。
        /// </summary>
        private void DeleteReminder()
        {
            if (_reminder == null) { return; }

            if (MessageBoxHelper.ShowConfirmation("您确定要删除这个提醒吗？", true) == DialogResult.Yes)
            {
                if (ReminderInfo.Remove<ReminderInfo>(_reminder))
                {
                    this.Hide();
                    this.Dispose();
                }
                else
                {
                    MessageBoxHelper.ShowAlert("删除失败。");
                }
            }
        }

        protected override void OnDeleteButtonClick(object sender, EventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);

            DeleteReminder();
        }

        protected override void OnEditButtonClick(object sender, EventArgs e)
        {
            base.OnEditButtonClick(sender, e);

            UserSettings.ShellInstance.ExecuteAction("SharpDiary.Reminder", "d53dfd48-fb6f-408f-91d8-009f79e84c09", _reminder, this);
        }

        /// <summary>
        /// 更新提醒状态。
        /// </summary>
        /// <param name="statusCode"></param>
        private void UpdateReminderStatus(Int64 statusCode)
        {
            if (_reminder == null) { return; }

            _reminder.Status = statusCode;
            if (!ReminderInfo.Update<ReminderInfo>(_reminder))
            {
                MessageBoxHelper.ShowAlert("更新提醒状态失败。");
            }
            else
            {
                UpdateLogo(_reminder.Status);
            }
        }

        private void chbOnOff_CheckedChanged_1(object sender, EventArgs e)
        {
            UpdateReminderStatus((onOffButtonControl.Checked ? 1 : 0));
        }
    }
}
